package com.hxkcloud.patient.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.PictureConsultActivity;
import com.hxkcloud.patient.adapter.SearchRecordAdapter;
import com.hxkcloud.patient.beans.SearchRecordBean;
import com.hxkcloud.patient.beans.SearchRecordBeanList;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.patient.views.pulltorefreshlayout.PullableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/25.
 */
@SuppressLint("ValidFragment")
public class FinishFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener{
    private View view;
    private int type;// 0 已完成 ,1 已取消
    private int state;// 0 已回答,-1未回答
    private SearchRecordBeanList searchRecordBeanList;
    private List<SearchRecordBean> data;

    private PullToRefreshLayout refreshLayout;
    private PullableListView listView;

    private SearchRecordAdapter searchRecordAdapter;
    private int totalCount = 0;
    private int pageIndex = 1;
    private int pageSize = 10;
    private int ids = 0;
    public FinishFragment(int type,int state){
        this.type = type;
        this.state = state;
    }

//    public FinishFragment(){
//        super();
//    }
    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what){
            case MessageCode.NETWORKCODE_USERRECORD_SUCCESS:
                if (msg.obj!=null){
                    dismisssLoading();
                    searchRecordBeanList = (SearchRecordBeanList) msg.obj;
                    if (pageIndex == 1){
                        data.clear();
                        if (searchRecordBeanList.getCancellist().size()>0){
                            refreshLayout.refreshFinish(View.VISIBLE);
                        }else {
                            refreshLayout.refreshFinish(View.GONE);
                        }

                    }else {
                        refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                    }

                    data.addAll(searchRecordBeanList.getCancellist());
                    totalCount = searchRecordBeanList.getTotalCount();
                }

                searchRecordAdapter.notifyDataSetChanged();
                break;
            case MessageCode.NETWORKCODE_USERRECORD_FAILURE:

                break;
        }
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_finish,null);
        data = new ArrayList<>();
        initView();
        initData();
        initListener();

        return view;
    }

    @Override
    protected void initView() {
        searchRecordAdapter = new SearchRecordAdapter(getActivity(),data);
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refreshlayout_SearchRechrd_fragment);
        refreshLayout.setCanPull(true,true);
        listView = (PullableListView) view.findViewById(R.id.fragment_search);
        listView.setAdapter(searchRecordAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ids = data.get(position).getId();
                if (ids!=0){
                    Intent intent = new Intent(mContext, PictureConsultActivity.class);
                    intent.putExtra("id",ids);
                    intent.putExtra("isAchieve","已回答");
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        loading();
        // Log.i("data------->","type:"+type+"state:"+state);
        YiJiaHuNetWorkUtils.UserRecord(loginInfo.getUserId(),type+"",state+"",pageIndex,pageSize,handler);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        pageIndex = 1;
        loading();
        YiJiaHuNetWorkUtils.UserRecord(loginInfo.getUserId(),type+"",state+"",pageIndex,pageSize,handler);
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        int dataSize = 0;
        for (SearchRecordBean bean:data){
            dataSize += 1;
        }
        if (dataSize>=totalCount){
            toastMsg("没有更多了");
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }else {
            pageIndex++;
            loading();
            YiJiaHuNetWorkUtils.UserRecord(loginInfo.getUserId(),type+"",state+"",pageIndex,pageSize,handler);
        }
    }
}
